using System;
using System.ComponentModel;

namespace WB.Mvc.Forms
{
	/// <summary>
	/// Assumes that string properties named Body or Content should be text areas.
	/// This convention is not case-sensitive.
	/// </summary>
	public class TextAreaConventionFieldStrategy : IModelFieldStrategy
	{
		public virtual Field Create(string name)
		{
			if (String.IsNullOrEmpty(name))
				return null;

			if (name.Equals("Body", StringComparison.InvariantCultureIgnoreCase) || name.Equals("Content", StringComparison.InvariantCultureIgnoreCase))
				return new TextAreaField(name);

			return null;
		}

		/// <summary>
		/// Creates a field based on <see cref="PropertyDescriptor" />.
		/// Follows the convention that if a property is named body or content, it should be a text area.
		/// Returns <c>null</c> if the property is not named body or content.  This convention is not case-sensitive.
		/// </summary>
		/// <remarks>
		/// It is intended, though not required, that users of this strategy accept
		/// <c>null</c> return values as meaning that this field strategy
		/// does not wish to handle this <see cref="PropertyDescriptor"/>.
		/// </remarks>
		public Field Create(PropertyDescriptor descriptor)
		{
			if (descriptor.PropertyType == typeof(string))
				return Create(descriptor.Name);

			return null;
		}
	}
}